package com.baizhi.controller;

import com.baizhi.entity.Song;
import com.baizhi.service.SongService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("song")
public class SongController {
    @Autowired
    private SongService songService;
    @RequestMapping("select")
    @ResponseBody
    public Map selectPage(int page, int limit, String songName, Integer who){
        PageInfo<Song> songPageInfo = songService.selectAll(page,limit,songName,who);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",songPageInfo.getTotal());
        map.put("data",songPageInfo.getList());
        return map;
    }

    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file) throws Exception{
        String s2 = UUID.randomUUID().toString() + file.getOriginalFilename();
        file.transferTo(new File("d:\\images",s2));
        Map map = new HashMap();
        map.put("code",0);
        map.put("songPath",s2);
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Song song){

        Map map = new HashMap();
        try{
            songService.insert(song);
            map.put("isInsert",true);
        }catch (Exception e){
            map.put("isInsert",false);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(Integer id){
        Map map = new HashMap();
        try{
            songService.delete(id);
            map.put("isDelete",true);
        }catch (Exception e){
            map.put("isDelete",false);
        }
        return map;
    }
    @RequestMapping("deleteMany")
    @ResponseBody
    public Map delete(Integer[] ids){
        Map map = new HashMap();
        try{
            songService.deleteMany(ids);
            map.put("isDelete",true);
        }catch (Exception e){
            map.put("isDelete",false);
        }
        return map;
    }
    @RequestMapping("selectOne")
    @ResponseBody
    public Song selectOne(Integer id){
        Song song = songService.selectOne(id);
        return song;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map delete(Song song){

        Map map = new HashMap();
        try{
            songService.update(song);
            map.put("isUpdate",true);
        }catch (Exception e){
            map.put("isUpdate",false);
        }
        System.out.println("map = " + map);
        return map;
    }
}
